package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/6/10 0010.
 */

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Role role) {
        if(role.getId() == null){
            roleMapper.insert(role);
        }else{
            roleMapper.updateByPrimaryKey(role);
        }
    }

    @Override
    public PageResult query(QueryObject queryObject) {
        //1 如果数据库中没有数据 需要分页吗? 肯定是不需要
        // selectForCount 方法返回值, 表中的数据条数
        int count = roleMapper.selectForCount(queryObject);
        if(count == 0){
            return PageResult.DEFAULT_PAGE;
        }
        // 那么思考 如何查看数据库表是否有数据存在
        // 2 分页操作
        List<Role> list = roleMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }
}
